package com.orange.delivery.dao;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import com.orange.delivery.model.persistent.Market;

public interface MarketRepository extends JpaRepository<Market, Integer>, MarketRepositoryCustom {

	// Spring Data JPA will execute the following JPQL Query under the hood :
	// SELECT x FROM Market x where name LIKE :("%"+name+"%))
	public List<Market> findByNameContaining(String name);

	/**
	 * Return a page of markets whose names contain a matching string
	 * 
	 * @param pageInfo
	 * @param name
	 *            a string to match with market names
	 * @return a page of markets
	 */
	public Page<Market> findByNameContaining(Pageable pageInfo, String name);

	@Query("SELECT COUNT(x) FROM Market x where x.name LIKE CONCAT('%',CONCAT(?1,'%'))")
	public long countMarketWithNameContaining(String name);

}
